<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>订单防重复消费系统设计指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .drop-cap::first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        .section-divider {
            position: relative;
            text-align: center;
            margin: 4rem 0;
        }
        .section-divider::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: linear-gradient(to right, transparent, #e5e7eb, transparent);
        }
        .section-divider span {
            background: white;
            padding: 0 2rem;
            position: relative;
            font-weight: 300;
            color: #9ca3af;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .concept-card {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-fadeInUp">
                <i class="fas fa-shield-alt mr-4"></i>订单防重复消费系统设计
            </h1>
            <p class="text-xl md:text-2xl font-light mb-8 animate-fadeInUp" style="animation-delay: 0.2s;">
                构建高可靠性的分布式订单处理系统
            </p>
            <div class="max-w-3xl mx-auto text-lg leading-relaxed animate-fadeInUp" style="animation-delay: 0.4s;">
                <p class="drop-cap">
                    防止订单被重复消费需要在系统设计时充分考虑并发控制、数据一致性和幂等性等问题。通过使用分布式锁、幂等性设计、数据库唯一约束和消息队列等技术手段，可以有效避免订单的重复消费，确保订单处理过程的可靠性和一致性。
                </p>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-7xl mx-auto px-6 py-12">
        <!-- System Architecture Visualization -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold text-center mb-8 text-gradient">系统架构概览</h2>
            <div class="bg-white rounded-2xl shadow-xl p-8">
                <div class="mermaid">
                    graph TB
                        A[客户端请求] --> B{API网关}
                        B --> C[订单服务]
                        C --> D[分布式锁]
                        C --> E[幂等性校验]
                        C --> F[数据库]
                        F --> G[唯一约束]
                        C --> H[消息队列]
                        H --> I[消费者服务]
                        I --> J[防重令牌]
                        I --> K[乐观锁]
                        style A fill:#667eea,stroke:#fff,color:#fff
                        style B fill:#764ba2,stroke:#fff,color:#fff
                        style C fill:#f093fb,stroke:#fff,color:#fff
                        style D fill:#4facfe,stroke:#fff,color:#fff
                        style E fill:#4facfe,stroke:#fff,color:#fff
                        style F fill:#43e97b,stroke:#fff,color:#fff
                        style G fill:#38f9d7,stroke:#fff,color:#fff
                        style H fill:#fa709a,stroke:#fff,color:#fff
                        style I fill:#fee140,stroke:#333,color:#333
                        style J fill:#30cfd0,stroke:#fff,color:#fff
                        style K fill:#30cfd0,stroke:#fff,color:#fff
                </div>
            </div>
        </section>

        <!-- Core Concepts Grid -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold text-center mb-12 text-gradient">核心防重策略</h2>
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
                <!-- 幂等性设计 -->
                <div class="card-hover bg-white rounded-xl shadow-lg p-6">
                    <div class="text-4xl text-purple-600 mb-4">
                        <i class="fas fa-fingerprint"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">幂等性设计</h3>
                    <p class="text-gray-600 mb-4">无论操作执行多少次，结果都保持一致。通过状态机管理订单生命周期。</p>
                    <div class="bg-purple-50 rounded-lg p-3">
                        <code class="text-sm text-purple-700">PENDING → PROCESSING → COMPLETED</code>
                    </div>
                </div>

                <!-- 分布式锁 -->
                <div class="card-hover bg-white rounded-xl shadow-lg p-6">
                    <div class="text-4xl text-blue-600 mb-4">
                        <i class="fas fa-lock"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">分布式锁</h3>
                    <p class="text-gray-600 mb-4">使用Redis或Zookeeper实现分布式环境下的资源互斥访问。</p>
                    <div class="bg-blue-50 rounded-lg p-3">
                        <code class="text-sm text-blue-700">SETNX order:lock:12345</code>
                    </div>
                </div>

                <!-- 防重令牌 -->
                <div class="card-hover bg-white rounded-xl shadow-lg p-6">
                    <div class="text-4xl text-green-600 mb-4">
                        <i class="fas fa-key"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">防重令牌</h3>
                    <p class="text-gray-600 mb-4">为每个订单生成唯一标识符，确保同一订单只被处理一次。</p>
                    <div class="bg-green-50 rounded-lg p-3">
                        <code class="text-sm text-green-700">UUID: 550e8400-e29b...</code>
                    </div>
                </div>

                <!-- 数据库约束 -->
                <div class="card-hover bg-white rounded-xl shadow-lg p-6">
                    <div class="text-4xl text-pink-600 mb-4">
                        <i class="fas fa-database"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">数据库唯一约束</h3>
                    <p class="text-gray-600 mb-4">利用数据库层面的唯一索引，从根本上防止重复记录。</p>
                    <div class="bg-pink-50 rounded-lg p-3">
                        <code class="text-sm text-pink-700">UNIQUE(order_id, status)</code>
                    </div>
                </div>
            </div>
        </section>

        <!-- Detailed Strategies -->
        <section class="mb-16">
            <div class="section-divider">
                <span><i class="fas fa-star"></i> 高级策略详解 <i class="fas fa-star"></i></span>
            </div>

            <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 mt-12">
                <!-- 消息队列幂等处理 -->
                <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                    <div class="flex items-center mb-6">
                        <div class="text-3xl text-indigo-600 mr-4">
                            <i class="fas fa-envelope-open-text"></i>
                        </div>
                        <h3 class="text-2xl font-bold">消息队列幂等处理</h3>
                    </div>
                    <p class="text-gray-700 leading-relaxed mb-6">
                        在使用消息队列处理订单消费时，通过消息的唯一标识符来实现幂等性。每处理一条消息时，记录该消息的标识符，并检查是否已经处理过相同标识符的消息。
                    </p>
                    <div class="bg-indigo-50 rounded-xl p-6">
                        <h4 class="font-semibold text-indigo-800 mb-3">实现要点：</h4>
                        <ul class="space-y-2 text-indigo-700">
                            <li><i class="fas fa-check-circle mr-2"></i>消息去重表设计</li>
                            <li><i class="fas fa-check-circle mr-2"></i>消息ID生成策略</li>
                            <li><i class="fas fa-check-circle mr-2"></i>处理状态持久化</li>
                        </ul>
                    </div>
                </div>

                <!-- 乐观锁 -->
                <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                    <div class="flex items-center mb-6">
                        <div class="text-3xl text-teal-600 mr-4">
                            <i class="fas fa-sync-alt"></i>
                        </div>
                        <h3 class="text-2xl font-bold">乐观锁机制</h3>
                    </div>
                    <p class="text-gray-700 leading-relaxed mb-6">
                        通过版本号或时间戳来控制并发操作。在订单表中添加版本号字段，每次更新订单状态时，检查并更新版本号，避免并发冲突。
                    </p>
                    <div class="bg-teal-50 rounded-xl p-6">
                        <h4 class="font-semibold text-teal-800 mb-3">SQL示例：</h4>
                        <pre class="text-sm text-teal-700 overflow-x-auto">
UPDATE orders 
SET status = 'COMPLETED', 
    version = version + 1 
WHERE order_id = ? 
  AND version = ?</pre>
                    </div>
                </div>

                <!-- 延迟队列 -->
                <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                    <div class="flex items-center mb-6">
                        <div class="text-3xl text-orange-600 mr-4">
                            <i class="fas fa-clock"></i>
                        </div>
                        <h3 class="text-2xl font-bold">延迟队列</h3>
                    </div>
                    <p class="text-gray-700 leading-relaxed mb-6">
                        将订单放入延迟队列中，延迟一定时间后再处理订单状态。结合分布式锁或幂等性检查，确保超时订单只能被处理一次。
                    </p>
                    <div class="bg-orange-50 rounded-xl p-6">
                        <h4 class="font-semibold text-orange-800 mb-3">应用场景：</h4>
                        <ul class="space-y-2 text-orange-700">
                            <li><i class="fas fa-arrow-right mr-2"></i>订单超时自动取消</li>
                            <li><i class="fas fa-arrow-right mr-2"></i>支付确认延迟处理</li>
                            <li><i class="fas fa-arrow-right mr-2"></i>库存预占释放</li>
                        </ul>
                    </div>
                </div>

                <!-- 双写一致性校验 -->
                <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                    <div class="flex items-center mb-6">
                        <div class="text-3xl text-red-600 mr-4">
                            <i class="fas fa-balance-scale"></i>
                        </div>
                        <h3 class="text-2xl font-bold">双写一致性校验</h3>
                    </div>
                    <p class="text-gray-700 leading-relaxed mb-6">
                        